
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>Go 语言Map(集合) · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="../part18/" />
    
    
    <link rel="prev" href="../part16/" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../part1/">
            
                <a href="../part1/">
            
                    
                    Go 语言简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../part2/">
            
                <a href="../part2/">
            
                    
                    Go 语言环境安装
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../part3/">
            
                <a href="../part3/">
            
                    
                    Go 语言结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../part4/">
            
                <a href="../part4/">
            
                    
                    Go 语言基础语法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../part5/">
            
                <a href="../part5/">
            
                    
                    Go 语言数据类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../part6/">
            
                <a href="../part6/">
            
                    
                    Go 语言变量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../part7/">
            
                <a href="../part7/">
            
                    
                    Go 语言常量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../part8/">
            
                <a href="../part8/">
            
                    
                    Go 语言运算符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="../part9/">
            
                <a href="../part9/">
            
                    
                    Go 语言条件语句
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="../part10/">
            
                <a href="../part10/">
            
                    
                    Go 语言循环语句
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="../part11/">
            
                <a href="../part11/">
            
                    
                    Go 语言函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="../part12/">
            
                <a href="../part12/">
            
                    
                    Go 语言数组
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="../part13/">
            
                <a href="../part13/">
            
                    
                    Go 语言指针
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="../part14/">
            
                <a href="../part14/">
            
                    
                    Go 语言结构体
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16" data-path="../part15/">
            
                <a href="../part15/">
            
                    
                    Go 语言切片(Slice)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17" data-path="../part16/">
            
                <a href="../part16/">
            
                    
                    Go 语言范围(Range)
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.18" data-path="./">
            
                <a href="./">
            
                    
                    Go 语言Map(集合)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19" data-path="../part18/">
            
                <a href="../part18/">
            
                    
                    Go 语言递归函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.20" data-path="../part19/">
            
                <a href="../part19/">
            
                    
                    Go 语言类型转换
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.21" data-path="../part20/">
            
                <a href="../part20/">
            
                    
                    Go 语言接口
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.22" data-path="../part21/">
            
                <a href="../part21/">
            
                    
                    Go 错误处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.23" data-path="../part22/">
            
                <a href="../part22/">
            
                    
                    Go 并发
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >Go 语言Map(集合)</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="go-&#x8BED;&#x8A00;map&#x96C6;&#x5408;">Go &#x8BED;&#x8A00;Map(&#x96C6;&#x5408;)</h1>
<p>&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;Map &#x662F;&#x4E00;&#x79CD;&#x65E0;&#x5E8F;&#x7684;&#x952E;&#x503C;&#x5BF9;&#x7684;&#x96C6;&#x5408;&#x3002;Map &#x6700;&#x91CD;&#x8981;&#x7684;&#x4E00;&#x70B9;&#x662F;&#x901A;&#x8FC7; key &#x6765;&#x5FEB;&#x901F;&#x68C0;&#x7D22;&#x6570;&#x636E;&#xFF0C;key &#x7C7B;&#x4F3C;&#x4E8E;&#x7D22;&#x5F15;&#xFF0C;&#x6307;&#x5411;&#x6570;&#x636E;&#x7684;&#x503C;&#x3002;</p>
<p>&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;Map &#x662F;&#x4E00;&#x79CD;&#x96C6;&#x5408;&#xFF0C;&#x6240;&#x4EE5;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x50CF;&#x8FED;&#x4EE3;&#x6570;&#x7EC4;&#x548C;&#x5207;&#x7247;&#x90A3;&#x6837;&#x8FED;&#x4EE3;&#x5B83;&#x3002;&#x4E0D;&#x8FC7;&#xFF0C;Map &#x662F;&#x65E0;&#x5E8F;&#x7684;&#xFF0C;&#x6211;&#x4EEC;&#x65E0;&#x6CD5;&#x51B3;&#x5B9A;&#x5B83;&#x7684;&#x8FD4;&#x56DE;&#x987A;&#x5E8F;&#xFF0C;&#x8FD9;&#x662F;&#x56E0;&#x4E3A; Map &#x662F;&#x4F7F;&#x7528; hash &#x8868;&#x6765;&#x5B9E;&#x73B0;&#x7684;&#x3002;</p>
<h2 id="&#x5B9A;&#x4E49;-map">&#x5B9A;&#x4E49; Map</h2>
<p>&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x5185;&#x5EFA;&#x51FD;&#x6570; make &#x4E5F;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; map &#x5173;&#x952E;&#x5B57;&#x6765;&#x5B9A;&#x4E49; Map:</p>
<pre><code class="lang-go"><span class="hljs-comment">/* &#x58F0;&#x660E;&#x53D8;&#x91CF;&#xFF0C;&#x9ED8;&#x8BA4; map &#x662F; nil */</span>
<span class="hljs-keyword">var</span> map_variable <span class="hljs-keyword">map</span>[key_data_type]value_data_type

<span class="hljs-comment">/* &#x4F7F;&#x7528; make &#x51FD;&#x6570; */</span>
map_variable := <span class="hljs-built_in">make</span>(<span class="hljs-keyword">map</span>[key_data_type]value_data_type)
</code></pre>
<p>&#x5982;&#x679C;&#x4E0D;&#x521D;&#x59CB;&#x5316; map&#xFF0C;&#x90A3;&#x4E48;&#x5C31;&#x4F1A;&#x521B;&#x5EFA;&#x4E00;&#x4E2A; nil map&#x3002;nil map &#x4E0D;&#x80FD;&#x7528;&#x6765;&#x5B58;&#x653E;&#x952E;&#x503C;&#x5BF9;</p>
<p>&#x5B9E;&#x4F8B;</p>
<pre><code class="lang-go"><span class="hljs-keyword">package</span> main

<span class="hljs-keyword">import</span> <span class="hljs-string">&quot;fmt&quot;</span>

<span class="hljs-keyword">func</span> main() {
    <span class="hljs-keyword">var</span> countryCapitalMap <span class="hljs-keyword">map</span>[<span class="hljs-keyword">string</span>]<span class="hljs-keyword">string</span> <span class="hljs-comment">/* &#x521B;&#x5EFA;&#x96C6;&#x5408; */</span>
    countryCapitalMap = <span class="hljs-built_in">make</span>(<span class="hljs-keyword">map</span>[<span class="hljs-keyword">string</span>]<span class="hljs-keyword">string</span>)

    <span class="hljs-comment">/* map&#x63D2;&#x5165;key - value&#x5BF9;&#xFF0C;&#x5404;&#x4E2A;&#x56FD;&#x5BB6;&#x5BF9;&#x5E94;&#x7684;&#x9996;&#x90FD; */</span>
    countryCapitalMap [ <span class="hljs-string">&quot;France&quot;</span> ] = <span class="hljs-string">&quot;&#x5DF4;&#x9ECE;&quot;</span>
    countryCapitalMap [ <span class="hljs-string">&quot;Italy&quot;</span> ] = <span class="hljs-string">&quot;&#x7F57;&#x9A6C;&quot;</span>
    countryCapitalMap [ <span class="hljs-string">&quot;Japan&quot;</span> ] = <span class="hljs-string">&quot;&#x4E1C;&#x4EAC;&quot;</span>
    countryCapitalMap [ <span class="hljs-string">&quot;India&quot;</span> ] = <span class="hljs-string">&quot;&#x65B0;&#x5FB7;&#x91CC;&quot;</span>

    <span class="hljs-comment">/* &#x4F7F;&#x7528;&#x952E;&#x8F93;&#x51FA;&#x5730;&#x56FE;&#x503C; */</span>
    <span class="hljs-keyword">for</span> country := <span class="hljs-keyword">range</span> countryCapitalMap {
        fmt.Println(country, <span class="hljs-string">&quot;&#x9996;&#x90FD;&#x662F;&quot;</span>, countryCapitalMap [country])
    }

    <span class="hljs-comment">/* &#x67E5;&#x770B;&#x5143;&#x7D20;&#x5728;&#x96C6;&#x5408;&#x4E2D;&#x662F;&#x5426;&#x5B58;&#x5728; */</span>
    capital, ok := countryCapitalMap [ <span class="hljs-string">&quot;American&quot;</span> ] <span class="hljs-comment">/*&#x5982;&#x679C;&#x786E;&#x5B9A;&#x662F;&#x771F;&#x5B9E;&#x7684;,&#x5219;&#x5B58;&#x5728;,&#x5426;&#x5219;&#x4E0D;&#x5B58;&#x5728; */</span>
    <span class="hljs-comment">/* fmt.Println(capital) */</span>
    <span class="hljs-comment">/* fmt.Println(ok) */</span>
    <span class="hljs-keyword">if</span> ok {
        fmt.Println(<span class="hljs-string">&quot;American &#x7684;&#x9996;&#x90FD;&#x662F;&quot;</span>, capital)
    } <span class="hljs-keyword">else</span> {
        fmt.Println(<span class="hljs-string">&quot;American &#x7684;&#x9996;&#x90FD;&#x4E0D;&#x5B58;&#x5728;&quot;</span>)
    }
}
</code></pre>
<p>&#x4EE5;&#x4E0A;&#x5B9E;&#x4F8B;&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#x4E3A;&#xFF1A;</p>
<pre><code class="lang-sh">France &#x9996;&#x90FD;&#x662F; &#x5DF4;&#x9ECE;
Italy &#x9996;&#x90FD;&#x662F; &#x7F57;&#x9A6C;
Japan &#x9996;&#x90FD;&#x662F; &#x4E1C;&#x4EAC;
India  &#x9996;&#x90FD;&#x662F; &#x65B0;&#x5FB7;&#x91CC;
American &#x7684;&#x9996;&#x90FD;&#x4E0D;&#x5B58;&#x5728;
</code></pre>
<hr>
<h2 id="delete-&#x51FD;&#x6570;">delete() &#x51FD;&#x6570;</h2>
<p>delete() &#x51FD;&#x6570;&#x7528;&#x4E8E;&#x5220;&#x9664;&#x96C6;&#x5408;&#x7684;&#x5143;&#x7D20;, &#x53C2;&#x6570;&#x4E3A; map &#x548C;&#x5176;&#x5BF9;&#x5E94;&#x7684; key&#x3002;&#x5B9E;&#x4F8B;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">package</span> main

<span class="hljs-keyword">import</span> <span class="hljs-string">&quot;fmt&quot;</span>

<span class="hljs-keyword">func</span> main() {
    <span class="hljs-comment">/* &#x521B;&#x5EFA;map */</span>
    countryCapitalMap := <span class="hljs-keyword">map</span>[<span class="hljs-keyword">string</span>]<span class="hljs-keyword">string</span>{<span class="hljs-string">&quot;France&quot;</span>: <span class="hljs-string">&quot;Paris&quot;</span>, <span class="hljs-string">&quot;Italy&quot;</span>: <span class="hljs-string">&quot;Rome&quot;</span>, <span class="hljs-string">&quot;Japan&quot;</span>: <span class="hljs-string">&quot;Tokyo&quot;</span>, <span class="hljs-string">&quot;India&quot;</span>: <span class="hljs-string">&quot;New delhi&quot;</span>}

    fmt.Println(<span class="hljs-string">&quot;&#x539F;&#x59CB;&#x5730;&#x56FE;&quot;</span>)

    <span class="hljs-comment">/* &#x6253;&#x5370;&#x5730;&#x56FE; */</span>
    <span class="hljs-keyword">for</span> country := <span class="hljs-keyword">range</span> countryCapitalMap {
        fmt.Println(country, <span class="hljs-string">&quot;&#x9996;&#x90FD;&#x662F;&quot;</span>, countryCapitalMap[country])
    }

    <span class="hljs-comment">/* &#x5220;&#x9664;&#x5143;&#x7D20; */</span>
    <span class="hljs-built_in">delete</span>(countryCapitalMap, <span class="hljs-string">&quot;France&quot;</span>)
    fmt.Println(<span class="hljs-string">&quot;&#x6CD5;&#x56FD;&#x6761;&#x76EE;&#x88AB;&#x5220;&#x9664;&quot;</span>)

    fmt.Println(<span class="hljs-string">&quot;&#x5220;&#x9664;&#x5143;&#x7D20;&#x540E;&#x5730;&#x56FE;&quot;</span>)

    <span class="hljs-comment">/* &#x6253;&#x5370;&#x5730;&#x56FE; */</span>
    <span class="hljs-keyword">for</span> country := <span class="hljs-keyword">range</span> countryCapitalMap {
        fmt.Println(country, <span class="hljs-string">&quot;&#x9996;&#x90FD;&#x662F;&quot;</span>, countryCapitalMap[country])
    }
}
</code></pre>
<p>&#x4EE5;&#x4E0A;&#x5B9E;&#x4F8B;&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#x4E3A;&#xFF1A;</p>
<pre><code class="lang-sh">&#x539F;&#x59CB;&#x5730;&#x56FE;
France &#x9996;&#x90FD;&#x662F; Paris
Italy &#x9996;&#x90FD;&#x662F; Rome
Japan &#x9996;&#x90FD;&#x662F; Tokyo
India &#x9996;&#x90FD;&#x662F; New delhi
&#x6CD5;&#x56FD;&#x6761;&#x76EE;&#x88AB;&#x5220;&#x9664;
&#x5220;&#x9664;&#x5143;&#x7D20;&#x540E;&#x5730;&#x56FE;
Italy &#x9996;&#x90FD;&#x662F; Rome
Japan &#x9996;&#x90FD;&#x662F; Tokyo
India &#x9996;&#x90FD;&#x662F; New delhi
</code></pre>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="../part16/" class="navigation navigation-prev " aria-label="Previous page: Go 语言范围(Range)">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="../part18/" class="navigation navigation-next " aria-label="Next page: Go 语言递归函数">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"Go 语言Map(集合)","level":"1.18","depth":1,"next":{"title":"Go 语言递归函数","level":"1.19","depth":1,"path":"part18/README.md","ref":"part18/README.md","articles":[]},"previous":{"title":"Go 语言范围(Range)","level":"1.17","depth":1,"path":"part16/README.md","ref":"part16/README.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"part17/README.md","mtime":"2021-01-20T07:08:09.952Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2021-01-21T06:29:04.898Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

